﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Skywave.ComponentModel
{
    public class NumberConverter
    {
        private static string Get2DName(int x)
        {
            switch (x)
            {
                case 1:
                    return "یک";
                case 2:
                    return "دو";
                case 3:
                    return "سه";
                case 4:
                    return "چهار";
                case 5:
                    return "پنج";
                case 6:
                    return "شش";
                case 7:
                    return "هفت";
                case 8:
                    return "هشت";
                case 9:
                    return "نه";
                case 10:
                    return "ده";
                case 11:
                    return "یازده";
                case 12:
                    return "دوازده";
                case 13:
                    return "سیزده";
                case 14:
                    return "چهارده";
                case 15:
                    return "پانزده";
                case 16:
                    return "شانزده";
                case 17:
                    return "هفده";
                case 18:
                    return "هجده";
                case 19:
                    return "نوزده";
                default:
                    return "خارج از محدوده";
            }
        }
        private static string D2Name(int x)
        {
            switch (x)
            {
                case 2:
                    return "بیست";
                case 3:
                    return "سی";
                case 4:
                    return "چهل";
                case 5:
                    return "پنجاه";
                case 6:
                    return "شصت";
                case 7:
                    return "هفتاد";
                case 8:
                    return "هشتاد";
                case 9:
                    return "نود";
                default:
                    return "خارج از محدوده";
            }
        }
        private static string D3Name(int x)
        {
            switch (x)
            {
                case 1:
                    return "صد";
                case 2:
                    return "دویست";
                case 3:
                    return "سیصد";
                case 4:
                    return "چهارصد";
                case 5:
                    return "پانصد";
                case 6:
                    return "ششصد";
                case 7:
                    return "هفتصد";
                case 8:
                    return "هشتصد";
                case 9:
                    return "نهصد";
                default:
                    return "خارج از محدوده";
            }
        }
        private static string Get3DName(string x)
        {
            string tmp;
            int int1;
            bool isZero;
            isZero = true;
            for (int1 = 0; int1 < x.Length; int1++)
            {
                if (x[int1] != '0') isZero = false;
            }
            if (isZero)
            {
                return "";
            }
            else
            {
                if (x[0] == '0')
                {
                    tmp = Get3DName(x.Substring(1));
                }
                else
                {
                    switch (x.Length)
                    {
                        case 1:
                            tmp = Get2DName(int.Parse(x));
                            break;
                        case 2:
                            if (x[0] == '1')
                                tmp = Get2DName(int.Parse(x));
                            else
                            {
                                if (x[1] != '0')
                                    tmp = D2Name(int.Parse(x[0].ToString())) + " و " + Get2DName(int.Parse(x[1].ToString()));
                                else
                                    tmp = D2Name(int.Parse(x[0].ToString()));
                            }
                            break;
                        case 3:
                            if (x.Substring(1, 2) != "00")
                                tmp = D3Name(int.Parse(x[0].ToString())) + " و " + Get3DName(x.Substring(1, 2));
                            else
                                tmp = D3Name(int.Parse(x[0].ToString()));
                            break;
                        default:
                            tmp = "Out of bound";
                            break;
                    }
                }
                return tmp;
            }
        }
        private static string GetPartName(int x)
        {
            switch (x)
            {
                case 1:
                    return "";
                case 2:
                    return "هزار";
                case 3:
                    return "میلیون";
                case 4:
                    return "میلیارد";
                case 5:
                    return "تریلیارد";
                default:
                    return "تعریف نشده";
            }
        }
        private static string GetDName(string x)
        {
            string tmp, resault;
            int l, i;
            resault = "";
            l = x.Length;
            for (i = 1; i <= ((int)(l / 3) + 1); i++)
            {
                if (i != ((int)(l / 3) + 1))
                    tmp = Get3DName(x.Substring(l - (i * 3), 3));
                else
                    tmp = Get3DName(x.Substring(0, l - (i - 1) * 3));
                if (tmp != "")
                {
                    if (i != 1)
                        //resault += " و " + GetPartName(i) + " " + tmp;
                        //resault += tmp + " " + GetPartName(i) + " و ";
                        resault = tmp + " " + GetPartName(i) + " و " + resault;
                    else
                        resault = tmp + " و " + resault;
                }
            }
            if (resault.Length != 0) resault = resault.Remove(resault.Length - 3, 3);
            return resault;
        }
        //function GetPasvand(x:string):string;
        //var
        //   tmp:string;
        //begin
        //     tmp=copy(x,length(x),1);
        //     case tmp[1] of
        //     "2":
        //         GetPasvand="vom e ";
        //     "3":
        //         GetPasvand="vom e ";
        //     else
        //         GetPasvand="om e ";
        //     end;
        //end;

        public static string ToText(string stringNumber, bool withSuffix, Skywave.Globalization.LanguageTypes language)
        {
            switch (language)
            {
                case Skywave.Globalization.LanguageTypes.SystemDefault:
                    return "";
                case Skywave.Globalization.LanguageTypes.Persian:
                    string tmp = GetDName(stringNumber);
                    if (withSuffix)
                    {
                        string lastDigit = stringNumber.Substring(stringNumber.Length - 1, 1);
                        if (lastDigit == "3")
                            return tmp.Substring(0, tmp.Length - 1) + "وم";
                        else
                            return tmp + "م";
                    }
                    else
                        return tmp;
                case Skywave.Globalization.LanguageTypes.English:
                    return "";
                default:
                    return "";
            }
        }
        public static string ToText(int num, bool withSuffix, Skywave.Globalization.LanguageTypes language)
        {
            return ToText(num.ToString(), withSuffix, language);
        }
    }
}
